package com.newland.starpos.partner.controller;

import com.newland.starpos.partner.bean.dto.*;
import com.newland.starpos.partner.controller.advice.BaseController;
import com.newland.starpos.partner.exception.CustomException;
import com.newland.starpos.partner.http.ResponseModel;
import com.newland.starpos.partner.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping(value = "/information")
public class UserInfoController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 我的资料查询是否认证和是否绑定银行卡
     */
    @PostMapping(value = "/partner/realNameAndBankCard")
    public ResponseModel queryCertificate(){

        ResponseModel response = userInfoService.queryCertificate();
        return response;
    }

    /**
     * 我的资料实名认证
     */
    @RequestMapping(value = "/partner/realName", method = RequestMethod.POST)
    public ResponseModel authRealName(@RequestBody @Valid UserIdentityDTO userIdentityDTO,
                                      BindingResult bind, HttpServletRequest request) throws CustomException {

        checkBindResult(bind);
        ResponseModel response = userInfoService.authRealName(userIdentityDTO, request);
        return response;
    }

    /**
     * 我的资料银行卡绑定
     */
    @RequestMapping(value = "/partner/bankCard", method = RequestMethod.POST)
    public ResponseModel authBankCardSave(@RequestBody UserBankCardDTO userBankCardDTO
                                    , HttpServletRequest request) throws CustomException {

        ResponseModel response = userInfoService.bindOrUnbind(userBankCardDTO, request);
        return response;

    }

    /**
     * 我的资料银行卡所属行未查询
     */
    @RequestMapping(value = "/partner/unidentified", method = RequestMethod.POST)
    public @ResponseBody
    ResponseModel unidentifiedBankCardSave(@RequestBody UserBankCardDTO userBankCardDTO)
            throws CustomException {
        ResponseModel response = userInfoService.queryUnidentifiedList(userBankCardDTO);
        return response;

    }

    /**
     * 我的资料登录密码修改
     */
    @RequestMapping(value = "/partner/logPWD", method = RequestMethod.POST)
    public ResponseModel logResetPWD(@RequestBody PasswordDTO userPasswordDTO,
                              HttpServletRequest request) throws CustomException {
        ResponseModel response = userInfoService.modifyPassword(userPasswordDTO, request);
        return response;

    }

    /**
     * 我的资料验证原支付密码正确，之后在进入支付密码修改
     */
    @RequestMapping(value = "/partner/validatePaymentPWD", method = RequestMethod.POST)
    public @ResponseBody
    ResponseModel validatePaymentPWD(@RequestBody PasswordDTO userPasswordDTO)
            throws CustomException {
        ResponseModel response = userInfoService.queryvalidatepaymentPWD(userPasswordDTO);
        return response;

    }

    /**
     * 我的资料支付密码修改（原支付密码校验通过）
     */
    @RequestMapping(value = "/partner/paymentPWD", method = RequestMethod.POST)
    public @ResponseBody
    ResponseModel paymentPWD(@RequestBody PasswordDTO userPasswordDTO) throws CustomException {
        ResponseModel response = userInfoService.modifyPayPassword(userPasswordDTO);
        return response;

    }

    /**
     * 我的资料支付密码修改忘记密码(通过短信验证码,验证手机号和验证码,下发调用重置密码接口的凭证)
     */
    @RequestMapping(value = "/partner/forgetPaymentPWD", method = RequestMethod.POST)
    public @ResponseBody
    ResponseModel forgetPaymentPWD(@RequestBody PasswordDTO userPasswordDTO)
            throws CustomException {
        ResponseModel response = userInfoService.forgetPaymentPWD(userPasswordDTO);
        return response;

    }

    /**
     * 我的资料支付密码修改(校验原支付密码)
     */
    @RequestMapping(value = "/partner/validateCode", method = RequestMethod.POST)
    public ResponseModel validateCode(@RequestBody PasswordDTO userPasswordDTO)
            throws CustomException {

        ResponseModel response = userInfoService.validateCode(userPasswordDTO);
        return response;

    }

    /**
     * 我的资料(我的等级)
     */
    @RequestMapping(value = "/partner/grade", method = RequestMethod.POST)
    public @ResponseBody
    ResponseModel queryGrade(HttpServletRequest request) throws CustomException {
        ResponseModel response = userInfoService.queryGrade(request);
        return response;
    }

    /**
     * 收货地址查询
     */
    @RequestMapping(value = "/address/receiver", method = RequestMethod.GET)
    public @ResponseBody
    ResponseModel queryReceiver() throws CustomException {
        return userInfoService.queryReceiver();
    }

    /**
     * 收货地址新增或者修改
     */
    @RequestMapping(value = "/address/addmo", method = RequestMethod.POST)
    public @ResponseBody
    ResponseModel addOrModeReceiver(@RequestBody ReceiverDTO receiverDTO) throws CustomException {
        return userInfoService.addOrModeReceiver(receiverDTO);
    }

}
